package com.example.lancooznbk.view.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;

import com.example.lancooznbk.R;
import com.example.lancooznbk.presenter.SplashPresenter;
import com.example.lancooznbk.utils.RxHelper;
import com.example.lancooznbk.view.base.BaseActivity;
import com.example.lancooznbk.view.view_interface.ISplashView;
import com.lancoo.cpbase.authentication.activities.LoginActivity;
import com.lancoo.cpbase.authentication.base.LoginOperate;
import com.lancoo.reviewsystem.utils.ToastUtils;
import com.lancoo.rxretrofitlibrary.Api.UserInfo;

import rx.Subscriber;
import znbkMain.*;

/**
 * Created by Administrator on 2017/5/18.
 */
public class SplashActivity extends BaseActivity implements ISplashView {
    private SplashPresenter mMainPresenter;

    private final int SPLASH_WAIT_TIME = 0;//默认欢迎页面停留的时间

    private long loginTime;
    private LoginOperate mOperate;

//    private Handler handler;
//    private SharedPreferences sp;
//    private static final int DUR_DELAY=2500;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.SplashTheme);
        super.onCreate(savedInstanceState);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initViews() {
        loginTime = System.currentTimeMillis();
        mMainPresenter = new SplashPresenter(this);
        mOperate = new LoginOperate(SplashActivity.this);
        int result = mOperate.getCurrentUserState();
                if (result == 1) {

                    UserInfo.init();//初始化身份认证相关的数据
                    mMainPresenter.getData();
                }else if(result == 0){
                    //ToastUtils.showLong(getApplicationContext(),getString(R.string.no_user_message));
                    goLoginActivity();
                }else{
                   //ToastUtils.showLong(getString(R.string.network_error));
                   goLoginActivity();
                }

    }


    @Override
    protected void updateViews(boolean isRefresh) {

    }

    @Override
    public void goMainActivity(boolean isSucceed) {
        if (!isSucceed){
            goLoginActivity();
        }else {

            long currTime = System.currentTimeMillis();
            RxHelper.countdown((int) ((currTime - loginTime + SPLASH_WAIT_TIME) / 1000))
                    .compose(this.<Integer>bindToLifecycle())
                    .subscribe(new Subscriber<Integer>() {
                        @Override
                        public void onCompleted() {
                            doSkip();
                        }

                        @Override
                        public void onError(Throwable e) {
                            ToastUtils.showLong(getString(R.string.network_error));
                            goLoginActivity();
                        }

                        @Override
                        public void onNext(Integer integer) {

                        }
                    });
        }

    }

    @Override
    public void goLoginActivity(){

        mOperate.logOut();
        startActivity(new Intent(getHolderActivity(),LoginActivity.class));
        finish();
    }

    private void doSkip(){
        startActivity(new Intent(SplashActivity.this, ZnbkMainMenuActivity.class));
        overridePendingTransition(R.anim.hold, R.anim.zoom_in_exit);
        finish();


//        String USER_INFO_SP="";
//        try {
//            USER_INFO_SP=getPackageManager().getPackageInfo(getPackageName(), 0).packageName + "LOGIN_STATE_INFO";
//        } catch (PackageManager.NameNotFoundException e) {
//            e.printStackTrace();
//        }
//        // 判断首次登陆
//        sp=getSharedPreferences(USER_INFO_SP, Context.MODE_PRIVATE);
//        SharedPreferences.Editor editor=sp.edit();
//        if (sp.getBoolean("isFirst", true)) {
//            editor.putBoolean("isFirstRun", false);
//            editor.commit(); // 保存数据
//            // 检测跳转
//            handler=new Handler();
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    Intent intent=new Intent(SplashActivity.this, GuideViewActivity.class);
//                    startActivity(intent);
//
//                }
//            }, DUR_DELAY);
//        } else {
//            handler=new Handler();
//            handler.postDelayed(new Runnable() {
//                @Override
//                public void run() {
//                    Intent intent=new Intent(SplashActivity.this, ZnbkMainActivity.class);
//                    startActivity(intent);
//                }
//            }, DUR_DELAY);
//        }

    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        mOperate.stopService();
    }

    @Override
    public void onBackPressed() {
        //不响应后退键
        return;
    }
}